﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace API.Controls
{
    /// <summary>
    /// Interaction logic for MyInputControl.xaml
    /// </summary>
    public partial class MyInputControl : UserControl
    {
        protected bool isNumber = false;
        protected bool isEmail = false;

        protected bool isValid;

        protected int minLen = 0;
        protected int maxLen = 100;

        public MyInputControl()
        {
            InitializeComponent();
        }

        #region private function
        private bool CheckValidEmail()
        {
            isValid = true;
            Error.Text = "";
            if (!InputUtils.IsEmail(TextBox.Text, minLen, maxLen))
            {
                Error.Text = MyMessages.ERROR_EMAIL;
                isValid = false;
            }
            return isValid;
        }

        private bool CheckValidString()
        {
            int length = TextBox.Text.Length;

            Error.Text = "";
            isValid = length >= minLen && length <= maxLen;
            if (!isValid)
            {
                Error.Text = String.Format(MyMessages.ERROR_STRING, minLen, maxLen);
            }
            return isValid;
        }

        private bool CheckValidNumber(char temp)
        {
            isValid = true;
            Error.Text = "";

            if (!char.IsDigit(temp) && !char.IsControl(temp))
            {
                Error.Text = String.Format(MyMessages.ERROR_NUMBER, minLen, maxLen);
                isValid = false;
            }
            return isValid;
        } 
        #endregion
        
        #region public properties
        public bool IsNumber
        {
            get { return isNumber; }
            set { isNumber = value; }
        }

        public bool IsEmail
        {
            get { return isEmail; }
            set { isEmail = value; }
        }

        public int MinLength
        {
            get { return minLen; }
            set { minLen = value; }
        }

        public int MaxLength
        {
            get { return maxLen; }
            set { maxLen = value; }
        } 
        #endregion

        #region event function
        private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (isNumber)
            {
                CheckValidNumber((char)KeyInterop.VirtualKeyFromKey(e.Key));
            }
            else
            {
                CheckValidString();
            }

            if (!isValid)
            {
                e.Handled = true;
            }
        }

        private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if (isEmail)
            {
                CheckValidEmail();
            }
            if (!isValid)
            {
                e.Handled = true;
            }
        } 
        #endregion
    }
}
